package eu.telecombretagne.rsm.protocol.ABP.util;

import java.util.Observable;
import java.util.Observer;

import eu.telecombretagne.rsm.gui.NameConsts;
import eu.telecombretagne.rsm.gui.frames.DesktopFrame;

public class AutomataObserver implements Observer {
	private Boolean emission;
	
	/**
	 * The constructor of the AutomataObserver.
	 * @param pEmission If set to <i>true</i> observer is set for emitter. Otherwise is set for receiver.
	 */
	public AutomataObserver(Boolean pEmission) {
		emission = pEmission;
	}
	
	private void showAutomataState(String pState){
		if(emission == true){
			DesktopFrame.showAutomataStateEmitter(pState);
		} else if(emission == false){
			DesktopFrame.showAutomataStateReceiver(pState);
		}
	}
	
	private void logAutomataState(String pState){
		if(emission == true){
			DesktopFrame.writeLogInfo(NameConsts.EMITTERSTATE + pState);
		} else if(emission == false){
			DesktopFrame.writeLogInfo(NameConsts.RECEIVERSTATE + pState);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		AutomataState currentState = ((Automata) o).getState();
		String state = currentState.toString();
		
		showAutomataState(state);
		logAutomataState(state);
	}
}